package com.kt.demo.controller;
import com.kt.demo.pojo.DevUser;
import com.kt.demo.service.DataDictionaryService;
import com.kt.demo.service.DevService;
import com.kt.demo.service.Impl.AppCategoryService;
import com.kt.demo.tools.Constants;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/dev")
@Log4j2
public class DevController {
    @Autowired
    private DevService devUserService;
    @RequestMapping(value="/kk")
    public String ym(){
        log.debug("LoginController welcome index==================");
        return "index";
    }

    @RequestMapping(value="/login")
    public String login(){
        log.debug("LoginController welcome AppInfoSystem develpor==================");
        return "devlogin";
    }

    @RequestMapping(value="/dologin",method=RequestMethod.POST)
    public String doLogin(@RequestParam String devCode, @RequestParam String devPassword, HttpServletRequest request, HttpSession session){
        log.debug("doLogin====================================");
        //调用service方法，进行用户匹配
        DevUser user = null;
        try {
            user = devUserService.doLogin(devCode,devPassword);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(null != user){//登录成功
            //放入session
            session.setAttribute(Constants.DEV_USER_SESSION, user);
            //页面跳转（main.jsp）
            return "redirect:/dev/flatform/main";
        }else{
            //页面跳转（login.jsp）带出提示信息--转发
            request.setAttribute("error", "用户名或密码不正确");
            return "devlogin";
        }
    }

    @RequestMapping(value="/flatform/main")
    public String main(HttpSession session){
        if(session.getAttribute(Constants.DEV_USER_SESSION) == null){
            return "redirect:/dev/login";
        }
        return "developer/main";
    }

    @RequestMapping(value="/logout")
    public String logout(HttpSession session){
        //清除session
        session.removeAttribute(Constants.DEV_USER_SESSION);
        return "devlogin";
    }
}
